define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order_continue/index' + location.search,
                    add_url: 'order_continue/add?order_id='+ Fast.api.query('order_id'),
                    edit_url: 'order_continue/edit?order_id='+ Fast.api.query('order_id'),
                    del_url: 'order_continue/del',
                    multi_url: 'order_continue/multi',
                    table: 'order_continue',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        // {field: 'order_id', title: __('Order_id')},
                        {field: 'out_trade_no', title: __('Out_trade_no'), operate: 'like'},
                        {field: 'user.username', title: __('User_id'), operate: 'like'},
                        {field: 'time_slot', title: __('time_slot'), operate:false},
                        {field: 'days', title: __('Days'), operate: false},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'rental_money', title: __('Rental_money'), operate:'BETWEEN'},
                        // {field: 'deposit', title: __('Deposit'), operate:'BETWEEN'},
                        {field: 'all_money', title: __('All_money'), operate:'BETWEEN'},
                        {field: 'pay_type', title: __('Pay_type'), searchList: Config.payTypeList, formatter: Table.api.formatter.label},
                        {field: 'pay_status', title: __('Pay_status'), searchList: Config.statusList, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'order.out_trade_no', title: __('Total_trade_no')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                if (row.pay_status != 1 && row.continue_status == 3){
                                    $(table).data("operate-edit", null);
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            },
                            buttons: [
                                {
                                    name:'adopt',
                                    text:'通过',
                                    title:'通过',
                                    classname:'btn btn-xs btn-success btn-ajax',
                                    url: 'order_continue/examine?type=1',
                                    confirm: '确认通过？',
                                    visible: function (row) {
                                        if(row.pay_status == 1 && row.continue_status==1) return true;
                                        return false;
                                    },
                                    success:function (res){
                                        table.bootstrapTable('refresh');
                                    },
                                    error: function (data, ret) {
                                        return false;
                                    },
                                },
                                {
                                    name:'refuse',
                                    text:'拒绝',
                                    title:'拒绝',
                                    classname:'btn btn-xs btn-danger btn-ajax',
                                    url: 'order_continue/examine?type=2',
                                    confirm: '确认拒绝？',
                                    visible: function (row) {
                                        if(row.pay_status == 1 && row.continue_status==1) return true;
                                        return false;
                                    },
                                    success:function (res){
                                        table.bootstrapTable('refresh');
                                    },
                                    error: function (data, ret) {
                                        return false;
                                    },
                                },
                                {
                                    name:'cancel',
                                    text:'取消订单',
                                    title:'取消订单',
                                    classname:'btn btn-xs btn-success btn-dialog',
                                    url: 'order_continue/cancel',
                                    visible: function (row) {
                                        if((row.pay_status == 1 || row.pay_status == 2)  && (row.order.status == 1 || row.order.status == 2)) return true;
                                        return false;
                                    },
                                    success:function (res){
                                        table.bootstrapTable('refresh');
                                        Toastr.success("取消成功");
                                        layer.close(index);
                                        return false;
                                    },
                                    error: function (data, ret) {
                                        return false;
                                    },
                                },
                                // {
                                //     name:'return',
                                //     text:'退款',
                                //     title:'退款',
                                //     classname:'btn btn-xs btn-danger btn-dialog',
                                //     url: 'order_continue/return',
                                //     visible: function (row) {
                                //         if(row.pay_status == 2) return true;
                                //         return false;
                                //     },
                                //     success:function (res){
                                //         table.bootstrapTable('refresh');
                                //         Toastr.success("退款成功");
                                //         layer.close(index);
                                //         return false;
                                //     },
                                //     error: function (data, ret) {
                                //         return false;
                                //     },
                                // },
                                {
                                    name:'detail',
                                    text:'订单详情',
                                    title:'订单详情',
                                    icon:'fa fa-list',
                                    classname:'btn btn-xs btn-primary btn-dialog',
                                    url: 'order_change/detail'
                                }
                            ]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        return: function () {
            Controller.api.bindevent();
        },
        cancel: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});